﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Frame
{
    public partial class FrmLogin : Form
    {
        public FrmLogin()
        {
            InitializeComponent();
        }
        public delegate void LoginChangedEventHandler(string Name);
        public event LoginChangedEventHandler LoginChanged;
        private void btnClose_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show("确定是否退出程序？", "请确认", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
            if(result == DialogResult.Yes)
            {
                System.Environment.Exit(0);//单击后关闭窗口
            }
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            
            string userName = this.txtUserName.Text.Trim();
            string userPwd = this.txtPassword.Text.Trim();
            string sql = string.Format("select count(1) from [User] where uName = '{0}' and uPassword = '{1}'", 
                userName, userPwd);
            
            int result = DataHelper.sqlExecuteScalar(sql);
            if (result == 1)
            {
                MessageBox.Show("恭喜你，登陆成功！");
                
                FrmMainForm.FrmMainForm frmMainForm= new FrmMainForm.FrmMainForm();
                LoginChanged += frmMainForm.onLoginChanged;
                LoginChanged(userName);
                frmMainForm.Show();
                this.Hide();
            }
            else if(userName == "" || userPwd == "")
            {
                MessageBox.Show("账号和密码不能为空！");

            }
            else
            {
                MessageBox.Show("对不起，账号不存在或者密码错误！");
                this.txtUserName.Clear();
                this.txtPassword.Clear();
            }
        }

        private void btn_rigister_Click(object sender, EventArgs e)
        {
            Perform.FrmRigister frmRigister = new Perform.FrmRigister();
            frmRigister.Show();
        }

        private void txtPassword_TextChanged(object sender, EventArgs e)
        {

        }

        private void FrmLogin_Load(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
        }
    }
}
